= kujaku do |k|
  - k.helper :journal_period_crit

= form_tag() do

  = field_set :details do
    = attributes_list(@account, stamps: false) do |l|
      - l.attribute :name, url: true
      - l.attribute :number, url: true
      - l.attribute :description

    = error_messages "account"

  = field_set :items do
    - currency = Preference[:currency]
    - precision = Nomen::Currency[currency].precision
    - [:period, :started_at, :stopped_at].each do |x|
      = hidden_field_tag(x, params[x])

    %span.action-form.btn-group.hidden
      = link_to :add_compensation_entry.tl, { controller: :journal_entries, action: :new, redirect: request.fullpath }, { class: 'btn btn-default', id: 'new-entry', name: :journal_entry_items_ids, data: { complete_link_with_checked_row_ids: '#mark_journal_entry_items input[type="checkbox"]', show_if_checked: 'two-or-more' } }

    .active-list-actions
      = mask_lettered_items_button :mark_journal_entry_items

    - items = @account.reconcilable_entry_items(params[:period], params[:started_on], params[:stopped_on])
    - partial_lettering = items.any?(&:partially_lettered?)
    .active-list#mark_journal_entry_items
      %table.list
        %thead
          %tr
            %th{ colspan: (partial_lettering ? 2 : 1) }= JournalEntryItem.human_attribute_name(:letter)
            %th.act
            %th= JournalEntry.human_attribute_name(:printed_on)
            %th= JournalEntry.model_name.human
            %th= JournalEntryItem.human_attribute_name(:name)
            %th= JournalEntryItem.human_attribute_name(:absolute_debit)
            %th= JournalEntryItem.human_attribute_name(:absolute_credit)

        %tfoot
          %tr#total.valid.total{data: {valid_if_equality_between: "#total-debit, #total-credit", submit_if_valid: "true"}}
            %th{colspan: 5 + (partial_lettering ? 1 : 0)}= :total.tl
            %td#total-debit{data:  {use: "tr.selected .debit", calculate_round: precision}}=  0
            %td#total-credit{data: {use: "tr.selected .credit", calculate_round: precision}}= 0

        %tbody
          - items.each do |item|
            - checked = (item.letter.blank? && (params[:journal_entry_item]||{}).keys.include?(item.id.to_s))
            - trid, conf = "e#{item.id}", {}
            %tr{id: trid, class: (item.closed? ? "disabled " : checked ? "selected " : "test") + cycle("even", "odd") + (item.completely_lettered? ? ' lettered-item' : '')}
              %td.chk
                - unless item.completely_lettered? || item.closed?
                  = check_box_tag("journal_entry_item[#{item.id}][to_mark]", "1", checked, id: "journal_entry_item_#{item.id}_to_mark", data: {add_class: "selected", add_class_to: "##{trid}"})
                  - conf = {data: {click: "#journal_entry_item_#{item.id}_to_mark"}}
              %td.ltr
                = item.letter
              %td.act
                - unless item.letter.blank? or item.closed?
                  = link_to({action: :unmark, id: @account.id, letter: item.letter}, class: "unmark", method: :post, data: {confirm: :are_you_sure.tl}) do
                    %i
                    = :unmark.ta
              %td.dat{conf}= item.printed_on.l
              %td.code{conf}= link_to(item.entry_number, {action: :show, controller: :journal_entries, id: item.entry_id}, keep: true)
              %td{conf}= item.name
              %td.dec.debit{conf}=  number_to_accountancy(item.absolute_debit, currency)
              %td.dec.credit{conf}= number_to_accountancy(item.absolute_credit, currency)
